package com.nontax.test.util;

import com.alibaba.fastjson.JSON;
import org.apache.commons.codec.binary.Base64;

import java.nio.charset.Charset;

public class Base64Util {
    private static Charset charset = Charset.forName("UTF-8");
    /**
     * 将对象转换成JSON格式，并进行BASE64编码
     * @param obj
     * @return
     */
    public static String encodeBase64String(Object obj){

        if(obj==null)
            return "";

        String json = JSON.toJSONString(obj);
        byte [] jsonByte = json.getBytes(charset);
        return Base64.encodeBase64String(jsonByte);
    }
    /**
     * BASE64解码
     */
    public static String decodeBase64(String str){
        byte [] paramByte = Base64.decodeBase64(str);
        return new String(paramByte,charset);
    }
}
